﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _04_理解事件02
{
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student(12, "小红", 90);
            Teacher t = new Teacher();
            s.SendScoreEvent += new Student.SendScore(t.Get);
            s.Send();
            Console.ReadKey();

        }
    }
    class Student
    {
        public int Id { get; private set; }
        public string  Name { get; private set; }
        public int Score { get; private set; }
        public Student(int id, string name, int score)
        {
            this.Id = id;
            this.Name = name;
            this.Score = score;
        }
        public delegate void SendScore(object sender, EventArgs e);
        public event SendScore SendScoreEvent;

      
        public void Send()
        {
            if (SendScoreEvent != null)
            {
                Console.WriteLine("----------发送以下内容----------");
                Console.WriteLine("学号：{0}，姓名：{1}，成绩：{2}", this.Id, this.Name, this.Score);
                SendScoreEvent(this, new EventArgs());//核心就在这里：运行事件封装的委托事件序列
            }
        }
        
    }
    class Teacher
    {
        public string Name { get; set; }

        public void Get(object sender, EventArgs e)
        {
            Student s = sender as Student;
            Console.WriteLine("----------接收以下内容----------");
            Console.WriteLine("学号：{0}，姓名：{1}，成绩：{2}",s.Id,s.Name,s.Score);
        }
    }
}
//谁是监视者，谁是被监视者，监视者绑定事件到被监视者的事件上面，同时监视者可以获取被监视者传递过来的当前对象
